package com.linghui.videoplus.ipai.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linghui.videoplus.ipai.R;
import com.linghui.videoplus.ipai.VideoPlusApplication;
import com.linghui.videoplus.ipai.api.json.JsonAPI;
import com.linghui.videoplus.ipai.bean.VideoBean;
import com.linghui.videoplus.ipai.utils.Constants;
import com.linghui.videoplus.ipai.utils.ImageLoaderTask;
import com.linghui.videoplus.ipai.utils.ImageLoaderTaskParam;
import com.linghui.videoplus.ipai.view.LazyScrollView;
import com.linghui.videoplus.ipai.view.LazyScrollView.OnScrollListener;

public class VideoCascadeActivity extends BaseActivity implements OnClickListener,OnTouchListener{

	private LazyScrollView waterfall_scroll;
	private LinearLayout waterfall_container;
	private ArrayList<LinearLayout> waterfall_items;
	private ImageView waterFallImage;
	private ImageView settingIV;
	
	private TextView videoDewcTextVidw;
	private Display display;
	private List<VideoBean> videoList;
	private List<VideoBean> fiterVideoList;
	private int itemWidth;
	private int column_count = 3;// 显示列数
	private int page_count = 15;// 每次加载15张图片
	private int current_page = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_video_cascade);
		
		menuLayout = (com.linghui.videoplus.ipai.view.MenuLayout) this.findViewById(R.id.menuLayout);
		menuLayout.setOnClickListener(this);
		menuLayout.setFocus(R.id.photoLL);
		
		settingIV=(ImageView)findViewById(R.id.settingIV);
		settingIV.setOnTouchListener(this);
		
		display = this.getWindowManager().getDefaultDisplay();
		itemWidth = display.getWidth() / column_count;// 根据屏幕大小计算每列大小
		InitLayout();
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(v==settingIV){
			if(event.getAction()==MotionEvent.ACTION_DOWN){
				settingIV.setBackgroundResource(R.drawable.btn_setting_down);
			}else if(event.getAction()==MotionEvent.ACTION_UP){
				settingIV.setBackgroundResource(R.drawable.btn_setting_up);
				SettingActivity.launch(VideoCascadeActivity.this,REQUEST_CODE_VIDEO_CASCADE, REQUEST_CODE_SETTING);
			}
		return true;
		}
		return true;
	}
	
	@Override
	public void onClick(View v) {
		int vId=v.getId();
		if(vId==R.id.photoLL||vId==R.id.photoImageView){
			VideoCascadeActivity.launch(this, REQUEST_CODE_DEFAULT,REQUEST_CODE_VIDEO_CASCADE);
		}else if(vId==R.id.cameraLL||vId==R.id.cameraImageView){
			CameraActivity.launch(this, REQUEST_CODE_DEFAULT, REQUEST_CODE_CAMERA);
		}else if(vId==R.id.homeLL||vId==R.id.homeImageView){
			BrowserActivity.launch(this,VideoPlusApplication.getUrlIndex(this),REQUEST_CODE_DEFAULT, REQUEST_CODE_DEFAULT);
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			VideoPlusApplication.exitApplication(this);
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void InitLayout() {
		waterfall_scroll = (LazyScrollView) findViewById(R.id.waterfall_scroll);
		waterfall_scroll.getView();
		waterfall_scroll.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onTop() {
			}
			@Override
			public void onScroll() {
			}
			@Override
			public void onBottom() {
				// 滚动到最低端
				if(videoList!=null){
					AddItemToContainer(++current_page, page_count);
				}
			}
		});

		waterfall_container = (LinearLayout)findViewById(R.id.waterfall_container);
		waterfall_items = new ArrayList<LinearLayout>();

		for (int i = 0; i < column_count; i++) {
			LinearLayout itemLayout = new LinearLayout(this);
			LinearLayout.LayoutParams itemParam = new LinearLayout.LayoutParams(
					itemWidth, LayoutParams.WRAP_CONTENT);
//			itemLayout.setPadding(0,0, 5, 0);
			itemLayout.setOrientation(LinearLayout.VERTICAL);

			itemLayout.setLayoutParams(itemParam);
			waterfall_items.add(itemLayout);
			waterfall_container.addView(itemLayout);
		}
		// 加载所有图片路径
		JsonAPI jsonAPI=new JsonAPI();
		videoList=jsonAPI.getMyVideo(true);
		// 第一次加载
		if(videoList!=null){
			fiterVideoList=new ArrayList<VideoBean>();
			for (int i = 0; i < videoList.size(); i++) {
				VideoBean bean=(VideoBean)videoList.get(i);
				File f=new File(bean.getVideoPath());
				if(f.exists()){
					fiterVideoList.add(bean);
				}
			}
			AddItemToContainer(current_page, page_count);
		}
	}
	
	private void AddItemToContainer(int pageindex, int pagecount) {
		int j = 0;
		int imagecount = fiterVideoList.size();
		for (int i = pageindex * pagecount; i < pagecount * (pageindex + 1)
				&& i < imagecount; i++) {
			j = j >= column_count ? j = 0 : j;
			AddImage(fiterVideoList.get(i), j++);
		}
	}
	
	private void AddImage(final VideoBean videoBean, int columnIndex) {
		LinearLayout item = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.adapter_waterfall_item, null);
		

		
		waterFallImage = (ImageView)item.findViewById(R.id.waterfall_image);
		videoDewcTextVidw=(TextView)item.findViewById(R.id.descTextView);
		String str="\""+videoBean.getDesc();
		
		if(str.length()>=13){
			str=str.substring(0, 13);
			str=str+"...";
		}
		str=str+"\"";
		videoDewcTextVidw.setText(str);
		waterFallImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				MyVideoActivity.launch(VideoCascadeActivity.this, REQUEST_CODE_VIDEO_CASCADE, REQUEST_CODE_MY_VIDEO,videoBean);
				Intent intent = new Intent(VideoCascadeActivity.this, MyVideoActivity.class);
				intent.putExtra(FROM_CODE_KEY, REQUEST_CODE_VIDEO_CASCADE);
				intent.putExtra(TO_CODE_KEY, REQUEST_CODE_MY_VIDEO);
				intent.putExtra(SERIALIZABLE__KEY, videoBean);
				VideoCascadeActivity.this.startActivity(intent);
				overridePendingTransition(R.anim.slide_left_in,R.anim.slide_left_out);
				VideoCascadeActivity.this.finish();
				
				
			}
		});
		
		
		waterfall_items.get(columnIndex).addView(item);
		ImageLoaderTaskParam param = new ImageLoaderTaskParam();
		param.setFilename(videoBean.getName());
		param.setItemWidth(itemWidth);
		param.setVideoBean(videoBean);
		param.setItem(item);
		ImageLoaderTask task = new ImageLoaderTask(waterFallImage);
		task.execute(param);
	}

	
	public static void launch(Activity mActivity,int fromCode,int toCode) {
		Intent intent = new Intent(mActivity, VideoCascadeActivity.class);
		intent.putExtra(FROM_CODE_KEY, fromCode);
		intent.putExtra(TO_CODE_KEY, toCode);
		mActivity.startActivity(intent);
		mActivity.finish();
	}

}
